/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import centralOperator from '../../../scripts/centralOperator'
import DefaultValueOperator from '../../../scripts/defaultValueOperator.js'

const JS_TAG = 'JS/Component/Service Item: ';
const FLAG_INSTALL_ON_DEMAND = 2048;
const FLAG_ABILITY_NEW_MISSION = 268435456;
const APP_LINK_TYPE = {
  TYPE_URL: 0,
  TYPE_RPK: 1,
  TYPE_H5: 2,
  TYPE_APP: 3,
  TYPE_FA: 4
}
const DEFAULT_IMAGE_PLACE_HOLDER = '/common/img/base/ic_public_picture.svg';

export default {
  props: ['width', 'height', 'item'],
  data: {
    imgStyle: '',
    placeHolderImagePath: '/common/img/base/ic_public_picture.svg',
  },
  computed: {
    imageWidth() {
      return this.width ? this.width : '78px'
    },
    imageHeight() {
      return this.height ? this.height : '78px'
    }
  },
  onInit() {
    this.placeHolderImagePath = DefaultValueOperator.imgPath(this.imagePath, DEFAULT_IMAGE_PLACE_HOLDER, this.$app.$def.globalData.isDarkMode);
  },
  onClickItem() {
    console.debug(JS_TAG + 'onServiceClick data= ' + JSON.stringify(this.item));
    switch (this.item.appLinkType) {
      case APP_LINK_TYPE.TYPE_URL:
      case APP_LINK_TYPE.TYPE_RPK:
      case APP_LINK_TYPE.TYPE_H5:
      case APP_LINK_TYPE.TYPE_APP:
        console.debug(JS_TAG + 'this.item.appLinkType = ' + this.item.appLinkType);
        centralOperator.openUrl({
          url: this.item.linkUrl
        });
        break;
      case APP_LINK_TYPE.TYPE_FA:
        centralOperator.startAbility({
          bundleName: this.item.packageName,
          abilityName: this.item.serviceName,
          intentParams: {},
          intentOptions: {
            flags: FLAG_INSTALL_ON_DEMAND | FLAG_ABILITY_NEW_MISSION
          }
        });
        break;
      default:
        console.debug(JS_TAG + 'other situations, this.item.appLinkType = ' + this.item.appLinkType);
        break;
    }
  }
}